const pathToRegexp = require('path-to-regexp')

var keys = []
var re_1 = pathToRegexp('/foo/:bar')
console.log(re_1)

//无修饰符 一个占位需要1个字符填坑
var re_2 = pathToRegexp('/:foo/:bar')
console.log('\/:foo\/:bar <=',(re_2.exec('/test')))
console.log('\/:foo\/:bar <=',(re_2.exec('/test/route')))

//? 一个占位 需要0个或1个字符填坑
var re_3 = pathToRegexp('/:foo/:bar?')
console.log('/:foo/:bar? <=',re_3.exec('/test'))
console.log('/:foo/:bar? <=',re_3.exec('/test/route'))
console.log('/:foo/:bar? <=',re_3.exec('/test/route/third'))

//* 一个占位 可以0个或多个字符填坑
var re_4 = pathToRegexp('/:foo*')
console.log('/:foo* <=',re_4.exec('/'))
console.log('/:foo* <=',re_4.exec('/bar/baz'))

//+ 一个占位 可以1个或多个字符填坑
var re_5 = pathToRegexp('/:foo+')
console.log('/:foo+ <=',re_5.exec('/'))
console.log('/:foo+ <=',re_5.exec('/bar/baz'))

//自定义
var re_6 = pathToRegexp('/icon-:foo(\\d+).png')
console.log('/icon-:foo(\\d+).png <=',re_6.exec('/icon-123.png'))
console.log('/icon-:foo(\\d+).png <=',re_6.exec('/icon-abc.png'))

//未命名坑名 无视分隔符`/`
var re_7 = pathToRegexp('/:foo/(.*)',keys)
console.log(keys)
console.log('/:foo/(.*) <=',re_7.exec('/test/route'))
console.log('/:foo/(.*) <=',re_7.exec('/test/route/third'))

//parse 返回所有的keys和那些没有占位符的
var tokens = pathToRegexp.parse('/route/:foo/(.*)')
console.log(tokens)
//tokensToRegExp  返回Regexp
console.log(pathToRegexp.tokensToRegExp(tokens))
var pathFn = pathToRegexp.tokensToFunction(tokens)
console.log(pathFn({foo:'123','0':'other'}))

var toPath = pathToRegexp.compile('/user/:id')
console.log(toPath({id:'123'}))
console.log(toPath({ id: '/' }))
console.log(toPath({ id: ':/' })) //=> "/user/%3A%2F"


